Skip to content

Conversation

@dianqk
Copy link
Member

@dianqk dianqk commented Dec 23, 2025

As an alternative to #150192.

Introduces a new pass that propagates the known ranges of SSA locals.
We can know the ranges of SSA locals at some locations for the following code:

fn foo(a: u32) {
  let b = a < 9;
  if b {
    let c = b; // c is true since b is whitin the range [1, 2)
    let d = a < 8; // d is true since b whitin the range [0, 9)
  }
}

This PR only implements a trivial range: we know one value on switch, assert, and assume.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 23, 2025
@Kobzol
Copy link
Member

Kobzol commented Dec 23, 2025

@bors try @rust-timer queue

(I want to test if try builds work and this PR seems like a good fit for a perf. run :) )

@rust-timer

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Dec 23, 2025
[EXPERIMENT] New MIR Pass: SsaRangePropagation
@rust-bors

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Dec 23, 2025
@rust-bors
Copy link
Contributor

rust-bors bot commented Dec 23, 2025

☀️ Try build successful (CI)
Build commit: d9c5f75 (d9c5f757034b39605aac2eeef1b90a892b2c647d, parent: 99ff3fbb86658b427f5dd7daaae8db5626a63c26)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (d9c5f75): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.3% [0.2%, 0.8%] 6
Regressions ❌
(secondary)
0.1% [0.0%, 0.3%] 8
Improvements ✅
(primary)
-0.4% [-0.9%, -0.2%] 7
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.1% [-0.9%, 0.8%] 13

Max RSS (memory usage)

Results (primary -0.9%, secondary 1.7%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.0% [1.9%, 2.1%] 2
Regressions ❌
(secondary)
4.2% [0.9%, 5.5%] 7
Improvements ✅
(primary)
-3.8% [-5.4%, -2.2%] 2
Improvements ✅
(secondary)
-1.9% [-2.9%, -1.5%] 5
All ❌✅ (primary) -0.9% [-5.4%, 2.1%] 4

Cycles

Results (primary 1.8%, secondary -1.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
1.8% [1.8%, 1.8%] 1
Regressions ❌
(secondary)
2.4% [2.3%, 2.6%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.2% [-6.2%, -1.2%] 5
All ❌✅ (primary) 1.8% [1.8%, 1.8%] 1

Binary size

Results (primary -0.0%, secondary -0.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.3% [0.1%, 0.7%] 4
Regressions ❌
(secondary)
0.1% [0.1%, 0.2%] 2
Improvements ✅
(primary)
-0.1% [-0.8%, -0.0%] 17
Improvements ✅
(secondary)
-0.1% [-0.8%, -0.0%] 16
All ❌✅ (primary) -0.0% [-0.8%, 0.7%] 21

Bootstrap: 482.582s -> 484.115s (0.32%)
Artifact size: 390.35 MiB -> 390.71 MiB (0.09%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Dec 23, 2025
@dianqk
Copy link
Member Author

dianqk commented Jan 2, 2026

@bors try parent=99ff3fbb86658b427f5dd7daaae8db5626a63c26 @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Jan 2, 2026
[EXPERIMENT] New MIR Pass: SsaRangePropagation
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jan 2, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 2, 2026

☀️ Try build successful (CI)
Build commit: edacb2e (edacb2e2bfe3e402771d6cc818d38a1c1e0aa524, parent: 99ff3fbb86658b427f5dd7daaae8db5626a63c26)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (edacb2e): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.3% [0.2%, 0.7%] 6
Regressions ❌
(secondary)
0.1% [0.0%, 0.2%] 7
Improvements ✅
(primary)
-0.4% [-0.9%, -0.1%] 10
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.1% [-0.9%, 0.7%] 16

Max RSS (memory usage)

Results (primary -0.2%, secondary 1.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.3% [2.0%, 2.6%] 3
Regressions ❌
(secondary)
3.8% [0.5%, 5.8%] 8
Improvements ✅
(primary)
-3.9% [-5.4%, -2.5%] 2
Improvements ✅
(secondary)
-1.9% [-3.0%, -1.5%] 5
All ❌✅ (primary) -0.2% [-5.4%, 2.6%] 5

Cycles

Results (primary 1.8%, secondary -1.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
1.8% [1.8%, 1.8%] 1
Regressions ❌
(secondary)
2.2% [2.1%, 2.3%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.4% [-6.3%, -1.2%] 5
All ❌✅ (primary) 1.8% [1.8%, 1.8%] 1

Binary size

Results (primary -0.0%, secondary -0.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.4% [0.1%, 0.7%] 4
Regressions ❌
(secondary)
0.1% [0.1%, 0.2%] 2
Improvements ✅
(primary)
-0.1% [-0.8%, -0.0%] 16
Improvements ✅
(secondary)
-0.1% [-0.8%, -0.0%] 16
All ❌✅ (primary) -0.0% [-0.8%, 0.7%] 20

Bootstrap: 482.582s -> 482.879s (0.06%)
Artifact size: 390.35 MiB -> 390.72 MiB (0.09%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jan 2, 2026
@rust-log-analyzer

This comment has been minimized.

@dianqk
Copy link
Member Author

dianqk commented Jan 7, 2026

The pass is fast, and it reduced MIR body definitely. All regressions are from opt, and there are probably more inlining.

@dianqk dianqk marked this pull request as ready for review January 7, 2026 13:23
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 7, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 7, 2026

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jan 7, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 7, 2026

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@dianqk dianqk added the A-mir-opt Area: MIR optimizations label Jan 7, 2026
@dianqk dianqk changed the title [EXPERIMENT] New MIR Pass: SsaRangePropagation New MIR Pass: SsaRangePropagation Jan 7, 2026
@cjgillot cjgillot self-assigned this Jan 7, 2026
Copy link
Contributor

@cjgillot cjgillot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for coding this! I don't have many comments on the design, this pass is very clear. I mostly have nits/suggestions on the implementation.

View changes since this review

if let Err(Some(place)) = self.simplify_operand(cond, location) {
let successor = Location { block: *target, statement_index: 0 };
if location.block != successor.block
&& self.unique_predecessors.contains(successor.block)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why check location.block != successor.block? We know that location.block has at least one predecessor which is reachable from bb0, so which is not itself. So if successor.block has a unique predecessor, it must be different from location.block, mustn't it?
I'm tempted to turn the location.block != successor.block into an assertion, or do you have a test to exhibit this case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, and I can change to location.dominates(successor, self.dominators) for assert.

}
let successor = Location { block: target, statement_index: 0 };
if location.block != successor.block
&& self.unique_predecessors.contains(successor.block)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, the unique_predecessors check should be equivalent to location.dominates(successor, self.dominators), isn't it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider the test case on_match_2 and on_if_2. They are not equivalent for the SwitchInt terminator.

}

let otherwise = Location { block: targets.otherwise(), statement_index: 0 };
if place.ty(self.local_decls, self.tcx).ty.is_bool()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind leaving a FIXME to extend this to other types?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have left a FIXME. Those FIXMEs can be fixed when it shows beneficial.

//! let b = a < 9;
//! if b {
//! let c = b; // c is true since b is within the range [1, 2)
//! let d = a < 8; // d is true since b within the range [0, 9)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand these comments. b is boolean, how can it be within a numeric range? And where does the [1, 2) come from?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the integer representation in MIR, so the full range of boolean is [0, 2).

@nnethercote
Copy link
Contributor

I have done a review pass but @cjgillot knows much more about MIR passes than I do and has already made good comments, so I will hand the review over:

r? @cjgillot

@rustbot
Copy link
Collaborator

rustbot commented Jan 8, 2026

Requested reviewer is already assigned to this pull request.

Please choose another assignee.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-mir-opt Area: MIR optimizations perf-regression Performance regression. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants